package com.gasolinator.a;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import com.gasolinator.a.app.GasolinatorData;
import com.gasolinator.a.app.activity.GasolinatorPreferencesActivity;
import com.gasolinator.a.app.activity.GasolinatorStationListActivity;
import com.gasolinator.a.app.activity.map.GasolinatorMapActivity;
import com.gasolinator.a.app.db.ChargeGasAsynkTask;
import com.gasolinator.a.app.helper.GasolinatorPreferences;
import com.gasolinator.a.augmented_reality.activity.SensorsActivity;
import com.gasolinator.a.augmented_reality.data.ARData;
import com.gasolinator.a.augmented_reality.data.GasolinatorDataSource;
import com.gasolinator.a.util.GasolinatorUtil;
import com.gasolinator.a.util.PromptUtils;
import com.gasolinator.a.util.persistence.DeviceIdGenerator;
import org.json.JSONException;

import java.util.Map;

public class GasolinatorActivity extends SensorsActivity
{
    private GasolinatorDataSource gasolinatorDataSource;
    private static PowerManager.WakeLock wakeLock = null;
    private GasolinatorPreferences gasolinatorPreferences;
    private float radius;
    private Map<Integer, String> gasStations;
    private ChargeGasAsynkTask gasStationsTask;


    public void onCreate(Bundle paramBundle)
    {
        super.onCreate(paramBundle);
        requestWindowFeature(1);
        requestWindowFeature(2);

        WindowManager windowManager = getWindowManager();
        if (windowManager == null ) {
            setContentView(R.layout.main);
        }
        else {
            int orientation = windowManager.getDefaultDisplay().getOrientation();
            if ((orientation == 1) || (orientation == 3))
                setContentView(R.layout.main_h);
            else
            {
                setContentView(R.layout.main);
            }
            gasolinatorDataSource = GasolinatorDataSource.getInstance(getApplicationContext());
        }
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
        gasolinatorPreferences= new GasolinatorPreferences(this);
        updateRadius();
        Location last = ARData.getCurrentLocation();
        gasolinatorDataSource.refresh(this, last, radius);
    }


    @Override
    public void onStart() {
        super.onStart();
        Location last = ARData.getCurrentLocation();
        updateRadius();
        if (gasolinatorPreferences.getUpdateOnStart()) {
            gasolinatorDataSource.refresh(this, last, radius);
        }

//        if (gasStationsTask == null || gasStationsTask.getStatus() != AsyncTask.Status.RUNNING) {
//            gasStationsTask = new ChargeGasAsynkTask(this);
//            gasStationsTask.execute();
//        }
    }

    @Override
    public void onResume() {
        super.onResume();
        wakeLock.acquire();
        if (gasolinatorPreferences.getProximityAlert()) {
            if (gasolinatorDataSource.getMarkers()!=null) {
//                final List<Marker> markers = gasolinatorDataSource.getGasolinatorMarkersFilteredByPrefs(this);
//                GeoPointHelper.addProximityAlertToGasStations(this,locationMgr, markers);
            }
        }
        updateRadius();
        //onRefreshAction(null);
    }

    private void updateRadius() {
        radius = gasolinatorPreferences.getRadius();
        ARData.setRadius(radius);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onPause() {
        super.onPause();
        wakeLock.release();
        GasolinatorUtil.hideWaitingGasolinatorMessage();
    }




    public boolean onCreateOptionsMenu(Menu paramMenu)
    {
        super.onCreateOptionsMenu(paramMenu);
        paramMenu.add(0, 0, 0, R.string.menu_set_location).setIcon(R.drawable.mylocation);     //menu_set_location
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem paramMenuItem)
    {
//      SearchAddressActivity.class;
        Intent intent = new Intent(this, GasolinatorMapActivity.class);
        startActivity(intent);

        return true;
    }

    public void onGasWalkerAction(View paramView)
    {
        Intent localIntent = new Intent(this, GasWalkerActivity.class);
        startActivity(localIntent);
    }

    public void onAboutAction(View paramView)
    {
        if (GasolinatorUtil.isNetworkAvailable(this)) {
            String deviceId = new DeviceIdGenerator(this).generate();
            Uri uri = Uri.parse("http://www.gasolinator.com/movil/?"+deviceId);
            Intent intent = new Intent("android.intent.action.VIEW", uri);
            startActivity(intent);
        } else {
            PromptUtils.showToastBase(this,getResources().getString(R.string.network_not_avaliable));
        }
    }

    public void onFavoritesAction(View paramView)
    {
        Intent intent = new Intent(this, GasolinatorStationListActivity.class);
        intent.putExtra("type", GasolinatorData.FAVORITES);
        startActivity(intent);
    }

    public void onListDistanceAction(View paramView)
    {
        Intent intent = new Intent(this, GasolinatorStationListActivity.class);
        intent.putExtra("type", GasolinatorData.DISTANCE);
        startActivity(intent);
    }
    public void onListPriceAction(View paramView)
    {
        Intent intent = new Intent(this, GasolinatorStationListActivity.class);
        intent.putExtra("type", GasolinatorData.PRICE);
        startActivity(intent);
    }


    public void onGasolinatorPreferencesAction(View paramView)
    {
        Intent intent = new Intent(this, GasolinatorPreferencesActivity.class);
        startActivity(intent);
    }

    public void onRefreshAction(View paramView) throws JSONException {

        gasolinatorDataSource.refresh(this,ARData.getCurrentLocation(),radius);
    }


    @Override
    public void onLocationChanged(Location location) {
        super.onLocationChanged(location);
//        Log.i("gasolinator Location has change =============================: ", location.toString());
//
//        String provider = location.getProvider();
//        final boolean satellite = gasolinatorPreferences.getSatellite();
//
//        Log.i("gasolinator provider detected    ====:",provider);
//        Log.i("gasolinator is satellite enabled ====:", String.valueOf(satellite));
//
//        if (provider.equals("ATL") && !satellite) {
//                GasolinatorUtil.showGPSDisabledAlertToUser(this);
//        } else {
//            gasolinatorDataSource.refresh(this,location,radius);
//        }
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onProviderEnabled(String s) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onProviderDisabled(String s) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public float getRadius() {
        return radius;
    }

    public void setGasStations(Map<Integer, String> gasStations)
    {
        this.gasStations=gasStations;
    }
}